package com.hdd.utils.wx;

import com.baidu.aip.contentcensor.AipContentCensor;
import com.baidu.aip.contentcensor.EImgType;
import com.hdd.utils.JsonUtil;
import lombok.Getter;
import lombok.Setter;
import org.json.JSONObject;

import java.util.List;
import java.util.Map;

/**
 * @author ;xuhu
 * @Description: 百度内容审核
 * @Date: 2020-09-24 16:39
 **/

public class ContentApprovalUtil {

    //设置APPID/AK/SK
    private static final String APP_ID = "22828602";
    private static final String API_KEY = "s0HyexBvUwSDTL9q8Kd3Y9Tb";
    private static final String SECRET_KEY = "YqPVfll5LOtsVQfEQRTc5Po1UVOUm7aX";
    private static AipContentCensor client;

    static {
        client = new AipContentCensor(APP_ID, API_KEY, SECRET_KEY);
    }

    public static ApprovalResult imagePath(String path) {
        JSONObject jsonObject = client.imageCensorUserDefined(path, EImgType.FILE, null);
        return handleData(jsonObject);
    }

    public static ApprovalResult imageUrl(String url) {
        JSONObject jsonObject = client.imageCensorUserDefined(url, EImgType.URL, null);
        return handleData(jsonObject);
    }

    public static ApprovalResult imageByte(byte[] file) {
        JSONObject jsonObject = client.imageCensorUserDefined(file, null);
        return handleData(jsonObject);
    }

    public static ApprovalResult textCheck(String text) {
        JSONObject jsonObject = client.textCensorUserDefined(text);
        return handleData(jsonObject);

    }

    private static ApprovalResult handleData(JSONObject data) {
        ApprovalResult approvalResult = new ApprovalResult();
        CheckData checkData = JsonUtil.parseObject(data.toString(), CheckData.class);
        String errorCode = checkData.getErrorCode();
        Integer conclusionType = checkData.getConclusionType();
        if (errorCode != null) {
            approvalResult.setSucceed(false);
            approvalResult.setMsg("审核失败");
        } else if (conclusionType != null && conclusionType == 1) {
            approvalResult.setSucceed(true);
            approvalResult.setMsg("合规");
        } else if (conclusionType != null) {
            StringBuilder msg = new StringBuilder();
            List<Map<String, Object>> list = checkData.getData();
            for (Map<String, Object> map : list) {
                msg.append(map.get("msg")).append(",");
            }
            approvalResult.setSucceed(false);
            approvalResult.setMsg(msg.subSequence(0, msg.lastIndexOf(",")).toString());
        }
        return approvalResult;
    }

    @Setter
    @Getter
    private static class CheckData {
        private String logId;
        private String errorCode;
        private String errorMsg;
        private String conclusion;
        private Integer conclusionType;
        private List<Map<String, Object>> data;

    }

    @Setter
    @Getter
    public static class ApprovalResult{
        private boolean isSucceed;
        private String msg;
    }

    public static void main(String[] args) {
        ApprovalResult approvalResult = textCheck("免费翻墙");
        System.out.println(approvalResult.msg);
    }
}